package com.zw.cloud.tools.threadlocal;

import com.zw.cloud.common.thread.pool.ThreadExecutorPool;
import org.apache.commons.lang3.StringUtils;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class InheritableThreadLocalUtil {

    private static final InheritableThreadLocal<String> THREAD_LOCAL = new InheritableThreadLocal<>();

    public static String getValue() {
        return THREAD_LOCAL.get();
    }

    public static void setValue(String value) {
        if (StringUtils.isNotBlank(value)) {
            THREAD_LOCAL.set(value);
        }
    }

    public static void clear() {
        THREAD_LOCAL.remove();
    }

    /*private static final InheritableThreadLocal<Map<String, String>> THREAD_LOCAL = new InheritableThreadLocal<>() {
        @Override
        protected Map<String, String> initialValue() {
            Map<String, String> map = new ConcurrentHashMap();
            return map;
        }
    };

    public static String getValue(String key) {
        return (String)((Map)THREAD_LOCAL.get()).get(key);
    }

    public static void setValue(String key, String value) {
        if (key != null && value != null) {
            ((Map)THREAD_LOCAL.get()).put(key, value);
        }
    }

    public static void clear() {
        ((Map)THREAD_LOCAL.get()).clear();
        THREAD_LOCAL.remove();
    }
    public static void main(String[] args) {
        //test1();
        test2();
    }*/

    public static void main(String[] args) {
        //test1();
        test2();
    }




    /**
     * InheritableThreadLocal在使用线程池时 线程上下文被污染
     */
    private static void test1() {
        int requestNum = 3;
        int currentRequest = 0;
        while (currentRequest++ < requestNum) {
            InheritableThreadLocalUtil.setValue(String.valueOf(currentRequest));
            System.out.println(String.format("处理开始,请求号=%s, value=%s", currentRequest,InheritableThreadLocalUtil.getValue()));

            int finalCurrentRequest = currentRequest;
            new Thread(() -> {
                System.out.println(String.format("异步场景下，请求号=%s, value=%s", finalCurrentRequest, InheritableThreadLocalUtil.getValue()));
            }).start();

            ThreadExecutorPool.msgThreadPoolExecutor.execute(() -> {
                System.out.println(String.format("异步场景下-pool，请求号=%s, value=%s", finalCurrentRequest, InheritableThreadLocalUtil.getValue()));
            });

            System.out.println(String.format("处理结束,请求号=%s, value=%s", currentRequest, InheritableThreadLocalUtil.getValue()));
            InheritableThreadLocalUtil.clear();
        }

        System.out.println("========================");
    }
    private static void test2() {
        // 异步处理线程池
        ExecutorService executorService = Executors.newSingleThreadExecutor();

        int requestNum = 3;
        int currentRequest = 0;
        while (currentRequest++ < requestNum) {
            // 请求前初始化traceContext
            InheritableThreadLocalUtil.setValue(String.valueOf(currentRequest));
            System.out.println(String.format("处理开始,请求号=%s, value=%s", currentRequest,InheritableThreadLocalUtil.getValue()));

            // 异步处理，传递traceContext
            int finalCurrentRequest = currentRequest;
            executorService.submit(() -> {
                System.out.println(String.format("异步场景下，请求号=%s, value=%s", finalCurrentRequest, InheritableThreadLocalUtil.getValue()));
            });

            // 请求处理结束清理traceContext
            System.out.println(String.format("处理结束,请求号=%s, value=%s", currentRequest, InheritableThreadLocalUtil.getValue()));
            InheritableThreadLocalUtil.clear();
        }
    }
}
